package cn.hexcloud.dga.governance.controller;

import cn.hexcloud.dga.governance.bean.GovernanceAssessDetail;
import cn.hexcloud.dga.governance.bean.GovernanceAssessGlobal;
import cn.hexcloud.dga.governance.bean.GovernanceAssessTecOwner;
import cn.hexcloud.dga.governance.service.GovernanceAssessDetailService;
import cn.hexcloud.dga.governance.service.GovernanceAssessGlobalService;
import cn.hexcloud.dga.governance.service.GovernanceAssessTecOwnerService;
import cn.hexcloud.dga.governance.service.MainAssessService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/governance")
public class GovernanceAssessController {
    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;

    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;

    @Autowired
    MainAssessService mainAssessService;

    /**
     *
     * {  "assessDate":"2023-04-01" ,"sumScore":90, "scoreList":[20,40,34,55,66] }
     *
     */
    @GetMapping("/globalScore")
    @CrossOrigin
    public  String getGlobalScore(){
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(new QueryWrapper<GovernanceAssessGlobal>().orderByDesc("assess_date").last("limit 1"));
        JSONObject jsonObject=new JSONObject();

        jsonObject.put("assessDate",governanceAssessGlobal.getAssessDate());
        jsonObject.put("sumScore",governanceAssessGlobal.getScore());

        List scoreList=new ArrayList();
        scoreList.add(governanceAssessGlobal.getScoreSpec());
        scoreList.add(governanceAssessGlobal.getScoreStorage());
        scoreList.add(governanceAssessGlobal.getScoreCalc());
        scoreList.add(governanceAssessGlobal.getScoreQuality());
        scoreList.add(governanceAssessGlobal.getScoreSecurity());

        jsonObject.put("scoreList",scoreList);

        return  jsonObject.toJSONString();
    }

    @GetMapping("/problemList/{governType}/{pageNo}/{pageSize}")
    @CrossOrigin
    public String getProblemList(@PathVariable("governType")String governType , @PathVariable("pageNo") Integer pageNo , @PathVariable("pageSize") Integer pageSize ){
        List<GovernanceAssessDetail> governanceAssessDetailList = governanceAssessDetailService.getLastProblemListByType(governType, pageNo, pageSize);

        return  JSONObject.toJSONString(governanceAssessDetailList) ;
    }

    @GetMapping("/problemNum")
    @CrossOrigin
    public String getProblemNum(){
        //求各个治理类型的问题个数
        List<Map<String, Object>> lastProblemNumMapList = governanceAssessDetailService.getLastProblemNum();

        //根据接口要求调整结构
        Map<String,Long> problemMap=new HashMap();
        for (Map<String, Object> map : lastProblemNumMapList) {
            String governanceType = (String)map.get("governance_type");
            Long problemNum = (Long)map.get("ct");
            problemMap.put(governanceType,problemNum);
        }
        return  JSONObject.toJSONString(problemMap) ;
    }

    @GetMapping("/rankList")
    @CrossOrigin
    public String getRankList() {
        QueryWrapper<GovernanceAssessTecOwner> queryWrapper = new QueryWrapper<GovernanceAssessTecOwner>().select("if(tec_owner is null ,'未分配',tec_owner) as tecOwner", "score")
                .inSql("assess_date", "select max(assess_date) from governance_assess_tec_owner").orderByDesc("score");
        List<Map<String, Object>> mapList = governanceAssessTecOwnerService.listMaps(queryWrapper);

        return JSONObject.toJSONString(mapList);

    }

    @PostMapping("/assess/{date}")
    @CrossOrigin
    public String getAssess(@PathVariable("date")String assessDate){
        mainAssessService.mainAssess(assessDate);
        return "success";
    }
}
